<template>
	<base-page class="base-form" title="我的课表" @back="onCancel()">
		<!-- 过滤组件 -->
		<!-- <view class="filter-box">
      <filter-box
        :sortList="sortList"
        :hasFilter="true"
        @refresh="onRefresh"
        @reset="onReset"
        @sort-change="onTableSortChange"
      >
        <u-row :gutter="0">
          <u-col :span="12">
            <daterange-filter
              label="上课时间"
              v-model="formFilter.attendClassTimeFilter"
              confirmText="确定"
              color="#ee0a24"
            />
          </u-col>
        </u-row>
      </filter-box>
    </view> -->

		<view class="main-box" style="padding: 0rpx 32rpx">
			<view>

				<my-calender :clearDate="true" :showMonth="false" :insert="true" :selected="selected" :lunar="false"
					:start-date="'1990-01-01'" :end-date="'2099-01-01'" @change="dateChange">
				</my-calender>
			</view>

			<!-- 列表组件 -->
			<view class="content-box">
				<view v-for="(item,index) in tableWidget.dataList" :key="index">
					<uni-card :title="item.title" :extra="item.addrs">
						<text class="uni-body" v-html="item.content" style="line-height: 60px;"></text>
						<uni-list>
							<uni-list-item :title="'上课时间：'+item.attendClassTime"></uni-list-item>
							<uni-list-item :title="'下课时间：'+item.quittingTime"></uni-list-item>
						</uni-list>
						<!-- courseInfo.professorId && courseInfo.isEvaluate != 1 -->
						<!-- isSign=1已签到   -->
						<view style="display: flex;justify-content: flex-end">
							<view v-if="item.isSign == 1">
								<span v-if="item.isEvaluate" style="color: #027bd5;">已评价</span>
								<button v-else type="primary" size="mini" style="background-color: #027bd5;"
									@click="toEvaluate(item)">去评价</button>
							</view>
							<view v-else>
								<span v-if="item.isLeave" style="color: #027bd5;">已请假</span>
								<button v-else type="primary" style="background-color: #027bd5;" size="mini"
									@click="toAskForLeave(item)">请假</button>
							</view>
						</view>
					</uni-card>
				</view>

			</view>
		</view>
	</base-page>
</template>

<script>
	import myCalender from "@/components/uni-calendar/components/uni-calendar/uni-calendar.vue"
	import rules from "@/utils/validate";
	import {
		stringCase,
		treeDataTranslate,
		findItemFromList,
		findTreeNodePath,
		findTreeNode,
		showConfirm,
		showMessage,
	} from "@/utils";
	import {
		uploadMixin,
		statsDateRangeMixin
	} from "@/utils/mixins";
	import {
		DropdownWidget,
		TableWidget,
		UploadWidget,
		ChartWidget,
	} from "@/utils/widget.js";
	import {
		CourseInfoController,
		ClassInfoController,
		TeacherInfoController,
		ProfessorInfoController,
		DictionaryController,
	} from "@/api";

	export default {
		name: "mobileStudentmyCourse",
		props: {
			subPage: {
				type: [Number, String, Boolean],
				default: 0,
			},
		},
		components: {
			myCalender
		},
		mixins: [uploadMixin, statsDateRangeMixin],
		data() {
			return {

				tableWidget: new TableWidget(
					this.loadCourseInfoWidgetData,
					this.loadCourseInfoVerify,
					true
				),
				formFilter: {
					attendClassTimeFilter: undefined,
				},
				formFilterCopy: {
					attendClassTimeFilter: undefined,
				},
				MobileStudentmyCourse: {
					isInit: false,
				},
				// 表格过滤字段
				sortList: [],
				selected: [{
					date: '2025-03-18', // date为所需要打点的日期
					type: "abnormal" // abnormal显示为橙色点
				}, {
					date: '2025-03-19',
					type: "abnormal" // normal显示为蓝色点
				}],

			};
		},
		methods: {
			toEvaluate(val) {
				uni.navigateTo({
					url: "/views/generated/evaluate?data=" + encodeURIComponent(JSON.stringify(val))
				})
			},
			toAskForLeave(val) {
				uni.navigateTo({
					url: "/views/generated/AskForLeave?data=" + encodeURIComponent(JSON.stringify(val))
				})
			},
			dateChange(e) {
				console.log(e);
				this.changeTime(e)
			},
			changeTime(val) {
				if (val.fulldate) {
					val = Date.parse(val.fulldate)

				} else {
					val = val
				}
				const params = {
					filter: {
						attendClassTime: val,
					},
				};

				CourseInfoController.getMyCourse(this, params)
					.then((res) => {
						this.tableWidget.dataList = res
					})
					.catch((e) => {});
			},

			changeTime2() {

				const params = {
					filter: {
						attendClassTime: null,
					},
				};
				this.selected = []
				CourseInfoController.getMyCourse(this, params)
					.then((res) => {
						for (var i = 0; i < res.length; i++) {
							this.selected.push({
								date: this.getFormatDate2(res[i].attendClassTime),
								type: "abnormal"
							});
						}
					})
					.catch((e) => {});
			},
			onCancel() {
				uni.navigateBack();
			},
			onRefresh() {
				// this.$refs.cardList.reset();

			},

			onShow() {
				this.changeTime(this.getTodayDate());
				this.changeTime2(null);
			},
			getTodayDate() {
				let date = new Date();
				let year = date.getFullYear();
				let month = String(date.getMonth() + 1).padStart(2, '0');
				let day = String(date.getDate()).padStart(2, '0');
				return `${year}-${month}-${day}`;
			},

			getFormatDate2(val) {
				let date = new Date(val);
				let year = date.getFullYear();
				let month = String(date.getMonth() + 1).padStart(2, '0');
				let day = String(date.getDate()).padStart(2, '0');
				return `${year}-${month}-${day}`;
			},
			/**
			 * 重置过滤值
			 */
			resetMobileStudentmyCourse() {
				this.formFilter.attendClassTimeFilter = undefined;
				this.formFilterCopy.attendClassTimeFilter = undefined;
			},
			/**
			 * 重置所有过滤值
			 */
			resetFilter() {
				this.resetMobileStudentmyCourse();
			},
			onReset() {
				this.resetFilter();
				this.onRefresh();
			},
			refreshMobileStudentmyCourse() {
				this.onRefresh();
			},
			/**
			 * 查询列表数据获取函数，返回Promise
			 */
			loadCourseInfoWidgetData(params) {
				if (params == null) params = {};
				params = {
					...params,
				};
				return new Promise((resolve, reject) => {
					CourseInfoController.list(this, params)
						.then((res) => {
							// console.log(res);

							resolve({
								dataList: res.dataList,
								totalCount: res.totalCount,
							});
						})
						.catch((e) => {
							reject(e);
						});
				});
			},
			/**
			 * 查询列表数据获取检测函数，返回true正常获取数据，返回false停止获取数据
			 */
			loadCourseInfoVerify() {
				this.formFilterCopy.attendClassTimeFilter =
					this.formFilter.attendClassTimeFilter;
				return true;
			},
			/**
			 * 排序改变
			 */
			onTableSortChange(sortInfo) {
				this.tableWidget.onSortChange(sortInfo);
				this.onRefresh();
			},
			/**
			 * 获取卡片图片地址
			 */
			getCardImageUrl(row) {
				let params = {
					id: row.id,
					fieldName: "content",
					asImage: true,
				};
				let fileList = this.parseUploadData(row.content, params);
				if (Array.isArray(fileList) && fileList.length > 0) {
					return fileList[0].url;
				}
				return "";
			},
			formInit() {},
		},
		mounted() {
			// 初始化页面数据
			this.formInit();
			this.onRefresh();
		},
	};
</script>

<style scoped>
	::v-deep .uni-calendar-item--checked {
		background-color: #027bd5;
	}

	::v-deep .uni-calendar-item--isDay {
		background-color: #027bd5;
	}

	/* ::v-deep .uni-calendar-item__weeks-box-circle-abnormal {
		background-color: #fdcea4;
	} */

	.content-box {
		margin: 0 -15px;
		/* margin-top: 40px; */
	}

	.sort-box {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
	}

	.sort-item {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
	}

	.sort-item+.sort-item {
		margin-left: 8px;
	}

	.nav-icon {
		display: inline;
	}

	.nav-icon+.nav-icon {
		margin-left: 5px;
	}
</style>